using System.Collections.Generic;
using Unity.Profiling;
using UnityEngine;

namespace Jobs.OOD
{
    /// <summary>
    /// WaveCubes（面向对象OOD版）
    /// </summary>
    public class WaveCubes : MonoBehaviour
    {
        public GameObject CubeArchetype;
        [Range(10, 100)] public int RadiusX;
        [Range(10, 100)] public int RadiusZ;
        List<Transform> mCubeLst;

        static readonly ProfilerMarker<int> mProfilerMarker =
            new ProfilerMarker<int>("WaveCubes UpdateTransform", "Objects Count");

        void Start()
        {
            mCubeLst = new List<Transform>();
            for (int x = -RadiusX; x < RadiusX; x++)
            {
                for (int z = -RadiusZ; z < RadiusZ; z++)
                {
                    var cube = Instantiate(CubeArchetype);
                    cube.transform.position = new Vector3(x * 1.1f, 0, z * 1.1f);
                    mCubeLst.Add(cube.transform);
                }
            }
        }

        void Update()
        {
            using (mProfilerMarker.Auto(mCubeLst.Count))
            {
                for (int i = 0; i < mCubeLst.Count; i++)
                {
                    var cubeTrans = mCubeLst[i];
                    var distance = Vector3.Distance(cubeTrans.position, Vector3.zero);
                    cubeTrans.localPosition += Vector3.up * Mathf.Sin(Time.time * 3f + distance * 0.2f);
                }
            }
        }
    }
}